package com.cxh.service.impl;

import com.cxh.base.Data2ServiceImpl;
import com.cxh.util.DataSource;
import com.cxh.dao.CarClassifyDao;
import com.cxh.dao.CarModelDao;
import com.cxh.entity.CarClassify;
import com.cxh.entity.CarModel;
import com.cxh.service.CarClassifyService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 类说明：学生表 
 * @author  YangHL
 * @date  2018-10-15 14:06:57
 */
@Service("carClassifyService")
@DataSource("dataSource2")
public class CarClassifyServiceImpl extends Data2ServiceImpl<CarClassify> implements CarClassifyService {
    @Autowired
    private CarClassifyDao carClassifyDao;
    @Autowired
    private CarModelDao carModelDao;


    @Override
    public List<CarClassify> loadPageList(Map<String, Object> map) {
       return carClassifyDao.loadPageList(map);
    }
    
    @Override
    public void insertImportData(String josnInfo) {
    	//解析json数据
    	JSONObject josnObject = JSONObject.fromObject(josnInfo);
    	Object resultObject = josnObject.get("result");
    	
    	JSONObject resultJson = JSONObject.fromObject(resultObject);
    	//车系类别ID
    	String carCategoryId = resultJson.get("I").toString();
    	
    	Object List = resultJson.get("List");
    	JSONArray ListArray = JSONArray.fromObject(List);
    	for (Object object : ListArray) {
			JSONObject infoJson = JSONObject.fromObject(object);
			//车型分类id
			String carClassifyId = infoJson.get("I").toString();
			//在售车型标识
			String forSale = infoJson.get("N").toString();
			
			CarClassify carClassify = new CarClassify();
			carClassify.setCarcategoryid(carCategoryId);
			carClassify.setCarclassifyid(carClassifyId);
			carClassify.setForsalename(forSale);
			//插入表中
			carClassifyDao.insert(carClassify);
			
			Object listObject = infoJson.get("List");
			JSONArray listArray = JSONArray.fromObject(listObject);
			for (Object listInfo : listArray) {
				JSONObject listJson = JSONObject.fromObject(listInfo);
				//年份
				String year = listJson.get("I").toString();
				Object object2 = listJson.get("List");
				JSONArray ListArray2 = JSONArray.fromObject(object2);
				for (Object object3 : ListArray2) {
					JSONObject list2Json = JSONObject.fromObject(object3);
					//车型id
					String carModelId = list2Json.get("I").toString();
					//车型名称
					String carModelName = list2Json.get("N").toString();
					//指导价
					String carPrice = list2Json.get("P").toString();
					
					CarModel carModel = new CarModel();
					carModel.setCarcategoryid(carCategoryId);
					carModel.setCarclassifyid(carClassifyId);
					carModel.setCarmodelid(carModelId);
					carModel.setCarmodelname(carModelName);
					carModel.setCarprice(carPrice);
					carModel.setCaryear(year);
					carModelDao.insert(carModel);
				}
				
			}
		}
    }

}
